{include file="public/header"}
<title>编辑</title>
</head>
<body>
<div class="page-container">
	<form class="form form-horizontal"  method="post">
		<input type="hidden" name="youhui_id" value="{$data.youhui_id}">

        <div class="row cl">
            <label class=" col-xs-3 col-sm-2"><span class="c-red">*</span>金额：</label>
            <div class=" col-xs-8 col-sm-9">
                <input type="number" class="form-control" placeholder="优惠金额" name="jine" value="{$data.jine}">
            </div>
        </div>
        <input type="hidden" name="fenbie" value="{$data['fenbie']}">
        <div class="row cl">
            <label class=" col-xs-3 col-sm-2"><span class="c-red">*</span>分别：</label>
            <div class="col-xs-8 col-sm-9">
                <select name="fenbie" class="form-control">
                    <option value="1" <?php if($data['fenbie']==1){ ?>selected="selected" <?php } ?> >优惠券</option>
                    <option value="2" <?php if($data['fenbie']==2){ ?>selected="selected" <?php } ?>>红包</option>

                </select>
            </div>
        </div>
        <div class="row cl">
            <label class=" col-xs-3 col-sm-2"><span class="c-red">*</span>类别：</label>
            <div class="col-xs-8 col-sm-9">
                <select name="leibie" class="form-control">
                      <option value="1" <?php if($data['leibie']==1){ ?>selected="selected" <?php } ?> >定金</option>
                      <option value="2" <?php if($data['leibie']==2){ ?>selected="selected" <?php } ?> >中期款</option>
                      <option value="3" <?php if($data['leibie']==3){ ?>selected="selected" <?php } ?> >尾款</option>
                      <option value="4" <?php if($data['leibie']==4){ ?>selected="selected" <?php } ?> >暂无</option>
                </select>
            </div>
        </div>
        <div class="row cl">
            <label class=" col-xs-3 col-sm-2"><span class="c-red">*</span>条件：</label>
            <div class=" col-xs-8 col-sm-9">
                <input type="number" class="form-control" placeholder="满足多少使用" name="tiaojan" value="{$data.tiaojan}">
            </div>
        </div>



        <div class="row cl">
            <label class=" col-xs-3 col-sm-2"><span class="c-red">*</span>时间：</label>
            <div class=" col-xs-8 col-sm-9">
                <input type="text" name="lalst_time" id="countTimestart" onfocus="selecttime(1)" size="17"
                       class="date input-text form-control" readonly
                       placeholder="有效时间" value="{$data.lalst_time? date('Y-m-d ',$data.lalst_time):' '}">

            </div>
        </div>
        <div class="row cl">
            <label class=" col-xs-4 col-sm-2"><span class="c-red">*</span>图片：</label>
            <div class=" col-xs-8 col-sm-9">
                <div id="uploader-demo">
                    <div id="fileList" class="uploader-list">
                        <div id="<?php echo uniqid('avatar')?>" class="file-item thumbnail">
                            <div onclick="delParent(this)" style="cursor: pointer;background: url({$Think.DOMAIN_SRC}/static/h-ui/images/del.png) no-repeat;background-size:contain;height: 20px;width: 20px;position: absolute;z-index: 10;right: 5px;"></div>
                            <img style="width: 100px;height: 100px;" src="{$data.logo}">
                            <input name="logo" type="hidden" value="{$data.logo}">
                            <div class="info"></div>
                        </div>
                    </div>
                    <div id="filePicker">选择图片</div>
                </div>

            </div>
        </div>
        <div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
				<button class="btn btn-primary radius" type="button"><i class="Hui-iconfont">&#xe632;</i> 提交</button>
			</div>
		</div>
	</form>
</div>

{include file="public/footer"}
<script type="text/javascript">
    $('.radius').click(function () {
        var src="{:Url('index/youhui/edit')}";
        $.ajax({
            type: 'POST',
            url: src,
            dataType: 'json',
            data:$('form').serializeArray(),
            success: function (data) {
                var res=JSON.parse(data);
                console.log(res);
                if(res.code==200){
                    layer.closeAll();
                    layer.msg(res.msg, {icon: 1, time: 2000}, function () {
                        setTimeout("window.parent.location.reload()", 1);
                    });
                }else{
                    layer.msg(res.msg,{icon: 5,time:1000});
                }

            },
            error: function (data) {
                console.log(data.msg);
            },
        });

    });
    function selecttime(flag) {
        if (flag == 1) {
            var endTime = $("#countTimeend").val();
            if (endTime != "") {
                WdatePicker({dateFmt: 'yyyy-MM-dd', maxDate: endTime})
            } else {
                WdatePicker({dateFmt: 'yyyy-MM-dd'})
            }
        } else {
            var startTime = $("#countTimestart").val();
            if (startTime != "") {
                WdatePicker({dateFmt: 'yyyy-MM-dd', minDate: startTime})
            } else {
                WdatePicker({dateFmt: 'yyyy-MM-dd'})
            }
        }
    }
    $("#img-change").click(function () {
        $("#file").click();
    });

    function delParent(object){
        $(object).parent().remove();
        count-=1;
    }
    var count=1;
    $(function(){
        var $list = $("#fileList");
        var uploader = WebUploader.create({
            auto: true,
            // 文件接收服务端。
            server: "{:Url('index/upload/upload_file')}",

            // 选择文件的按钮。可选。
            pick: '#filePicker',

            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize: false,
            duplicate:true,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            },
        });

        // 当有文件添加进来的时候
        uploader.on( 'fileQueued', function( file ) {
            var $li = $(
                '<div id="' + file.id + '" class="file-item thumbnail">' +
                '<div onclick="delParent(this)" style="cursor: pointer;background: url({$Think.DOMAIN_SRC}/static/h-ui/images/del.png) no-repeat;background-size:contain;height: 20px;width: 20px;position: absolute;z-index: 10;right: 5px;"></div>'+
                '<img style="width: 100px;height: 100px;">' +
                '<input name="logo" type="hidden" value="">'+
                '<div class="info">' + file.name + '</div>' +
                '</div>'
                ),
                $img = $li.find('img');


            // $list为容器jQuery实例
            $list.append( $li );

            // 创建缩略图
            // 如果为非图片文件，可以不用调用此方法。
            // thumbnailWidth x thumbnailHeight 为 100 x 100
            uploader.makeThumb( file, function( error, src ) {
                if ( error ) {
                    $img.replaceWith('<span>不能预览</span>');
                    return;
                }

                $img.attr( 'src', src );
            }, 100, 100 );
        });



        // 文件上传过程中创建进度条实时显示。
        uploader.on( 'uploadProgress', function( file, percentage ) {
            var $li = $( '#'+file.id ),
                $percent = $li.find('.progress span');

            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<p class="progress"><span></span></p>')
                    .appendTo( $li )
                    .find('span');
            }


            $percent.css( 'width', percentage * 100 + '%' );
        });
        var s=[];
        // 文件上传成功，给item添加成功class, 用样式标记上传成功。
        uploader.on( 'uploadSuccess', function( file,res ) {
            var data = JSON.parse(res._raw);
            if(data.errcode==300){
                layer.alert(data.msg);
            }
            var url = data.data;
            count+=1;
            $( '#'+file.id).find("logo").attr("src",url);
            $( '#'+file.id).find("input").val(url);
            $( '#'+file.id ).addClass('upload-state-done');
        });

        // 文件上传失败，显示上传出错。
        uploader.on( 'uploadError', function( file ) {
            var $li = $( '#'+file.id ),
                $error = $li.find('div.error');

            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }

            $error.text('上传失败');
        });

        // 完成上传完了，成功或者失败，先删除进度条。
        uploader.on( 'uploadComplete', function( file ) {
            $( '#'+file.id ).find('.progress').remove();
        });
    });


</script>
</body>
</html>
